﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Krist_em_up;
using Krist_em_up.Components;
using ZoomEngine.Animation;
using Krist_em_up.i18n;

namespace Krist_em_up.Interface
{
    public class Pause : GameScreen
    {
        protected TimeSpan timer;
        protected GameStateManager Game;
        protected Button[] buttons;
        protected ClockManager clockManager;

        protected SpriteBatch spriteBatch;

        protected SpriteFont font;

        protected Viewport PauseViewport, defaultViewport;

        protected int index;

        protected TimeSpan delay;

        protected Texture2D bg;

        protected ColorFromToAnimation colorAnim;

        public Pause(GameStateManager game)
            : base(game)
        {
            IsPopup = true;
            Game = game;
            clockManager = new ClockManager(game);
            delay = new TimeSpan(0, 0, 0, 0, 0);
            defaultViewport = Game.GraphicsDevice.Viewport;
            PauseViewport.Width = 4 * defaultViewport.Width / 5;
            PauseViewport.Height = 4 * defaultViewport.Height / 5;
            PauseViewport.X += defaultViewport.Width / 10;
            PauseViewport.Y += defaultViewport.Height / 10;
            spriteBatch = new SpriteBatch(game.GraphicsDevice);
            Reset();

            bg = Game.Content.Load<Texture2D>("Menu/pause-bg");
        }

        public override void Initialize()
        {

        }
        public override void LoadContent()
        {

        }
        public override void UnloadContent()
        {

        }
        public override void Update(GameTime gameTime)
        {
            Clavier kb = Game.getClavier();
            if (delay <= TimeSpan.Zero)
            {
                
                if (kb.IsKeyDown(Keys.Up))
                {
                    buttons[index].Unfocus();
                    index--;
                    if (index < 0)
                        index = buttons.Length - 1;
                    colorAnim.Pause();
                    colorAnim.Start();
                    delay = new TimeSpan(0, 0, 0, 0, 300);
                }
                else if (kb.IsKeyDown(Keys.Down))
                {
                    buttons[index].Unfocus();
                    index++;
                    if (index >= buttons.Length)
                        index = 0;
                    colorAnim.Pause();
                    colorAnim.Start();
                    delay = new TimeSpan(0, 0, 0, 0, 300);
                }
                if (kb.IsKeyDown(Keys.Enter) && kb.PreviousState.IsKeyUp(Keys.Enter))
                {
                    buttons[index].OnSelected();
                }
            }
            else
            {
                delay -= gameTime.ElapsedGameTime;
            }
            for (int i = 0; i < buttons.Length; i++)
            {
                buttons[i].Update(gameTime);
            }
            clockManager.Update(gameTime);


        }

        public override void Draw(GameTime gameTime)
        {
            Game.GraphicsDevice.Viewport = PauseViewport;
            Game.GraphicsDevice.Clear(new Color(201, 190, 190));

            spriteBatch.Begin();
            spriteBatch.Draw(bg, new Rectangle(0, 0, Game.GraphicsDevice.Viewport.Width, Game.GraphicsDevice.Viewport.Height), Color.White);
            spriteBatch.End();

            for (int i = 0; i < buttons.Length; i++)
            {
                buttons[i].Draw(spriteBatch, font);
            }

            Game.GraphicsDevice.Viewport = defaultViewport;

        }

        public override void Reset()
        {
            font = Game.Content.Load<SpriteFont>("Fonts/Texture");
            index = 0;
            buttons = new Button[]{
                new Button(new Vector2(PauseViewport.Width / 2 - font.MeasureString(Strings.reprendre).X / 2, PauseViewport.Height), Strings.reprendre),
                new Button(new Vector2(PauseViewport.Width / 2 - font.MeasureString(Strings.options).X / 2, -2*font.MeasureString(Strings.options).Y), Strings.options),
                new Button(new Vector2(PauseViewport.Width / 2 - font.MeasureString(Strings.quitter).X / 2, -font.MeasureString(Strings.quitter).Y), Strings.quitter),
            };
            buttons[0].Selected += Game.LeavePauseMenu;
            buttons[1].Selected += Game.MenuOptions;
            buttons[2].Selected += Game.LeaveGameFromPause;

            for (int i = 0; i < buttons.Count(); i++)
            {
                var item = buttons[i];
                var positionAnimation = new Vector2FromToAnimation(clockManager)
                {
                    From = item.Position,
                    To = new Vector2(item.Position.X, i*PauseViewport.Height/3),
                    Duration = 1.5,
                    ProgressTransform = ProgressTransforms.Bounce,
                    BeginOffset = 0.25 * i,
                    Apply = (v) => item.Position = v,
                };
                positionAnimation.Start();

            }
            buttons.ElementAt(0).Focus();
            colorAnim = new ColorFromToAnimation(clockManager)
            {
                From = Color.Lime,
                To = Color.Blue,
                Duration = 4,
                //ProgressTransform = ,
                AutoReverse = true,
                Apply = (c) => buttons[index].Color = c,
                RepeatCount = int.MaxValue,
            };
            colorAnim.Start();
            
        }

    }
}
